/**GTGrid工具类 (lxz)*/
/*显示提示*/
function showTip(obj){
	if(!$.trim($(obj).html()))
		return false;

	var X = $(obj).offset().top;
	var Y = $(obj).offset().left;
		
	$('#tipDiv').css("top",X+15);
	$('#tipDiv').css("left",Y+10);
	$('#tipDiv').css("width",$(obj).width()-10);
	$('#tipDiv').html($(obj).html());
		
	$('#tipDiv').show();
}

/*隐藏提示*/
function hideTip(obj){
	$('#tipDiv').hide();
}

/*GTGrid Cell 提示render*/
function tooltipRender(val,record){
	if(val && val!='null'){
		return '<div style="width:100%;white-space:nowrap;word-break:keep-all;overflow:hidden;text-overflow:ellipsis;" onmouseover="showTip(this)" onmouseout="hideTip(this)">'+val+'</div>';
	}else{
		return "";
	}
}

function getRealColumn(cell,grid){
	var classnames=cell.className.split(" ");
	var columnId="";
	if(classnames && classnames.length>0){
		for(var i=0;i<classnames.length;i++){
			if(classnames[i].indexOf("gt-col-"+grid.id+"-")!=-1){
				columnId= classnames[i].replace("gt-col-"+grid.id+"-","").toUpperCase();
				break;
			}
		}
	}
	
	if(columnId){
		classnames=grid.columns;
		for(var i=0;i<classnames.length;i++){
			if(classnames[i].id.toUpperCase()==columnId){
				return classnames[i];
			}
		}
	}
	return false;
}

function doSelectRow(value, record , cell, row, colNO, rowNO, columnObj, grid){
	var lastRow=$("#container"+grid.id).find("tr.gt-row-selected")//得到上次被选中的行
	 if(lastRow.size()!=0){
		  lastRow.removeClass("gt-row-selected");//去掉选中CSS
		  lastRow.find(":checkbox").removeAttr("checked");	 //去掉checkbox选中状态
	}
	
	var selRows=grid.selectedRows;

	if(selRows && selRows.length>0){
		 grid.unselectAllRow();
	}
	grid.selectRow(row, true);
	var selectRow= $("#container"+grid.id).find("tr[__gt_ds_index__="+rowNO+"]"); //得到当前被选中
	selectRow.addClass("gt-row-selected");
	selectRow.find(":checkbox").attr("checked","true");//加上checkbox选中状态
}


/*单击选中GTGrid行(最多只能选中一行)同一行单击两次会取消*/
function selectedSingleRow(value, record , cell, row, colNO, rowNO, columnObj, grid){
	var col=getRealColumn(cell,grid);
	if(col){
		columnObj=col;
	}
	
	if(columnObj.isCheckColumn && !isCheckRow(row)){
		grid.unselectAllRow();
		return;
	}
	if(!columnObj.isCheckColumn && isCheckRow(row)){
		//再次点击后取消息选中
		grid.unselectRow(row);
		var selectRow= $("#container"+grid.id).find("tr[__gt_ds_index__="+rowNO+"]")
		 selectRow.removeClass("gt-row-selected");//去掉选中CSS
		selectRow.find(":checkbox").removeAttr("checked");	
		return;
	}
	doSelectRow(value, record , cell, row, colNO, rowNO, columnObj, grid);
}

/*单击选中GTGrid行(最多只能选中一行)*/
function selectedSingleRow2(value, record , cell, row, colNO, rowNO, columnObj, grid){
	var col=getRealColumn(cell,grid);
	if(col){
		columnObj=col;
	}
	
	if(columnObj.isCheckColumn && !isCheckRow(row)){
		grid.unselectAllRow();
		return;
	}
	doSelectRow(value, record , cell, row, colNO, rowNO, columnObj, grid);
}

/*清除所有选中的记录*/
function clearAllSelectRow(grid){
	 var lastRow=$("#container"+grid.id).find("tr.gt-row-selected")//得到上次被选中的行
	 if(lastRow.size()!=0){
		  lastRow.removeClass("gt-row-selected");//去掉选中CSS
		  lastRow.find(":checkbox").removeAttr("checked");	 //去掉checkbox选中状态
	 }
	 grid.unselectAllRow();
}

/*单击选中GTGrid行(可选多行)*/
function selectedMultiRow(value, record , cell, row, colNO, rowNO, columnObj, grid){
	var col=getRealColumn(cell,grid);
	if(col){
		columnObj=col;
	}
	if(columnObj.isCheckColumn && !isCheckRow(row)){
		grid.unselectRow(row);
		return;
	}
	
	if(!columnObj.isCheckColumn && isCheckRow(row)){
		//再次点击后取消息选中
		grid.unselectRow(row);
		var selectRow= $("#container"+grid.id).find("tr[__gt_ds_index__="+rowNO+"]")
		 selectRow.removeClass("gt-row-selected");//去掉选中CSS
		selectRow.find(":checkbox").removeAttr("checked");	
		return;
	}else if(columnObj.isCheckColumn && isCheckRow(row)){
		return;
	}
	
	grid.selectRow(row, true);
	var selectRow=$("#container"+grid.id).find("tr[__gt_ds_index__="+rowNO+"]"); //得到当前被选中
	selectRow.find(":checkbox").attr("checked",true);//加上checkbox选中状态
	
}

/*行是否被选中*/
function isCheckRow(row){
	return $(row).hasClass("gt-row-selected");
}

/*GTGrid全选checkbox屏蔽,用法如(mygrid.onComplete=disableSelectAll)*/
function disableSelectAll(grid){
		var p=$("#container"+grid.id+" div input.gt-f-totalcheck").attr("disabled",true);
}

/*判断是否选中一行*/
function isSelectOne(mygrid){
	var rec = mygrid.getSelectedRecords();
	if(rec == null || rec.length != 1){
		return false;
	}
	else
		return true;
}


function escapeStr(str){
	return str.replace('"','\\"');
}

/**
 * 
 * @param grid Grid对象
 * @param dataProvider数据提供源,(默认是当前的数据提供源)
 * @param pageSize 每页显示多少条数据(默认按照当前设置)
 * @param pageNo 重置之后跳转到的页码(默认是1)
 * @param keepParam 是否保留最初的参数设置，默认是true
 * @param otherparam 其他参数
 */
function resetGrid(grid,formId,otherparam,dataProvider,pageSize,pageNo,keepParam)
{
	
	dataProvider = dataProvider ? dataProvider : _gt_grid_data_provider;
	pageSize = pageSize ? pageSize : grid.pageSize;
	pageNo = pageNo ? pageNo : 1;
	keepParam = keepParam ? keepParam : true;
	otherparam = otherparam ? otherparam : "";
	var _init = "";
	if(keepParam)
	{
		_init = _gt_grid_init_params;
	}
	var _new = "";
	if(formId){
		_new = map2String(Form.formToBean(formId));
	}
	var params = _init;
	
	if($.trim(_new) != "")
	{
		if($.trim(params) != "")
		{
			params += "," + _new;
		}else{
			params += _new;
		}
	}
	if($.trim(otherparam) != "")
	{
		if(params != "")
		{
			params += ",";
		}
		params += map2String(otherparam);
	}
	var map = grid.resetParameter(pageSize, params,pageNo);
	
	ajax.remoteCall(dataProvider,[map],function(reply){
		grid.cleanContent(); 
		//更新数据 
		grid.setContent(eval("(" + reply.getResult() + ")"));
	});
}

function map2String(map){
	var args='';
	if(!map){
		return args;
	}
	for(p in map){
		if(p)
			args+='"'+p+'":"'+escapeStr(map[p])+'",';
	}
	if(args.length>1)
		args=args.substring(0,args.length-1);
	return args;
}

/**
* 设置GTGrid的内容
* @param grid GTGrid对象
* @param queryResult ajax.remoteCall返回的结果
* @return
*/
function setGridContent(grid,queryResult){
	if(grid){
		grid.cleanContent();
		var content=eval("(" + queryResult + ")");
		var contentDiv=$('#'+grid.id+"_bodyDiv");
		var tipDiv=$('#'+grid.id+"_tipDiv");
		if(content.data.length==0){
			if(tipDiv.length==0){
				tipDiv=$('<div id="'+grid.id+'_tipDiv" style="width:100%;text-align: center;color:#CC3333;"><br/></br></br>未检索到您所需的数据！</div>').height($('#'+grid.id+'_bodyDiv').height());
				tipDiv.insertAfter(contentDiv);
			}
			contentDiv.hide();
			tipDiv.show();
		}else{
			grid.setContent(content);
			tipDiv.hide();
			contentDiv.show();
		}
	}
}

$().ready(function(){
	$('body').append('<div id="tipDiv" style="position: absolute;display:none;text-align:left; border: 1px solid green;z-index: 10000;width:100px;color: red;background-color: #FFF"></div>');
});